package com.example.exam.exception;

import com.example.exam.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author
 * @date 2022/7/22
 *
 * 统一异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler({Exception.class, Error.class})//捕获的异常
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)//改变返回的状态码
    public Object error(Throwable e) {
        log.error("error" + e);
        return R.fail("系统出错,请联系工作人员");
    }

    /**
     * 自定义异常处理
     */
    @ExceptionHandler(CustomException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object error(CustomException e) {
        log.error("error:" + e);
        return R.fail(e);
    }
}
